import 'package:erp/data/model/process.dart';
import 'package:erp/modules/process_center/tab_view/search_header.dart';
import 'package:erp/modules/process_center/widgets/flow_card.dart';
import 'package:erp/widgets/page_list/page_list.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'controller.dart';

class FlowTodo extends StatelessWidget {
  final List<Map<String, dynamic>> config = [
    {"label": "全部", "value": null},
    {"label": "未提交", "value": 0},
    {"label": "审批中", "value": 1},
    {"label": "已退回", "value": 2}
  ];

  List<Widget> _buildStatusTextList(List<Map<String, dynamic>> config, Rx<dynamic> currentValue) {
    return config
        .map(
          (e) => GestureDetector(
            onTap: () {
              currentValue.value = e["value"];
            },
            child: Container(
              alignment: AlignmentDirectional.centerStart,
              height: 50.h,
              decoration: BoxDecoration(
                border: Border(
                  top: BorderSide(
                    width: 1.w,
                    color: Color(0xFFDFE1E6),
                  ),
                ),
              ),
              child: Text(
                "${e["label"] ?? ""}",
                style: TextStyle(
                  color: Color(currentValue.value == e["value"] ? 0xff4C88FF : 0xFF666666),
                  fontSize: 24.sp,
                  fontWeight: FontWeight.w400,
                ),
              ),
            ),
          ),
        )
        .toList();
  }

  @override
  Widget build(BuildContext context) {
    var controller = Get.put(FlowTodoController());
    return Stack(
      children: [
        SearchHeader(
          onTapFilter: () {
            controller.showOverlay.value = !controller.showOverlay.value;
          },
          onTapSearch: (value) {
            controller.search(value);
          },
        ),
        Container(
          margin: EdgeInsets.only(top: 90.h),
          child: PageListWidget<Process, FlowTodoController>(
            createItem: (BuildContext context, int index, Process item) {
              return FlowTodoCard(
                process: item,
                onHandle: () {
                  controller.handle(item);
                },
                onDetail: () {
                  controller.detail(item);
                },
                onDelete: () {
                  controller.delete(item);
                },
                onEdit: () {
                  controller.edit(item);
                },
              );
            },
          ),
        ),
        Obx(
          () => controller.showOverlay.value
              ? Positioned(
                  top: 90.h,
                  left: 0,
                  right: 0,
                  bottom: 0,
                  child: Stack(
                    children: [
                      GestureDetector(
                        child: Container(
                          margin: EdgeInsets.only(top: 0.2),
                          decoration: BoxDecoration(
                            color: Color.fromRGBO(0, 0, 0, 0.4),
                          ),
                        ),
                        onTap: () {
                          controller.showOverlay.value = false;
                        },
                      ),
                      Container(
                        width: MediaQuery.of(context).size.width,
                        decoration: BoxDecoration(
                          color: Color(0xFFF2F7FF),
                          borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(20.r),
                            bottomRight: Radius.circular(20.r),
                          ),
                        ),
                        height: 250.h,
                        child: Padding(
                          padding: EdgeInsets.symmetric(horizontal: 35.w, vertical: 0),
                          child: Column(
                            children: [
                              ..._buildStatusTextList(config, controller.status),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                )
              : SizedBox.shrink(),
        )
      ],
    );
  }
}
